#include "drawing.h"
#include "mypushbutton.h"
#include <QGraphicsLineItem>
#include <QTimer>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include<QApplication>
#include<QPushButton>
#include<exception>
#include <QFileDialog>


DrawingScene::DrawingScene(QObject *parent)
    : QGraphicsScene(parent),m_view(nullptr)
{
    //设置绘画和擦除操作的初始值
    m_isErasing = false;
    m_isDrawing = false;
    // 设置画笔的初始颜色和宽度
    m_penColor = Qt::black;
    m_penWidth = 1;

}

void DrawingScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDrawing = true;
        m_lastPos = event->scenePos();

    } else if (event->button() == Qt::RightButton) {
        m_isErasing = true;
    }
}

void DrawingScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_isErasing) {
        // 清除轨迹上的图像
        QList<QGraphicsItem*> items = this->items(QRectF(event->scenePos() - QPointF(5, 5), QSizeF(20, 20)));
        for (QGraphicsItem *item : items) {
            if (item->type() == QGraphicsLineItem::Type) {
                removeItem(item);
                delete item;
            }
        }
    } else if (m_isDrawing) {

        // 绘制曲线
        QGraphicsLineItem *line = new QGraphicsLineItem();
         line->setPen(QPen(m_penColor, m_penWidth)); // 使用当前画笔颜色和宽度
        line->setLine(QLineF(m_lastPos, event->scenePos()));
        addItem(line);
        m_lastPos = event->scenePos();
        // 将当前图形项添加到当前操作中
        QList<QGraphicsItem*> itemsToAdd;
        itemsToAdd.append(line);
        m_currentOperation.append(itemsToAdd);
        // 发出信号表明场景已更改
        emit drawingchanged();
    }
}


void DrawingScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::RightButton) {
        m_isErasing = false;
    } else if (event->button() == Qt::LeftButton) {

        m_isDrawing = false;
        // 将当前操作添加到撤销栈中
        m_undoStack.append(m_currentOperation);
        // 清空前进栈
        m_redoStack.clear();
        // 发出信号表明场景已更改
        emit drawingchanged();
        // 清空当前操作
        m_currentOperation.clear();
    }
}
void DrawingScene::undo()
{
    if (canUndo()) {
        // 将当前图形项列表添加到前进栈中
        m_redoStack.append(m_undoStack.last());
        // 从撤销栈中弹出上一个图形项列表，并将其设置为当前图形项列表
        QList<QGraphicsItem*> itemsToRemove = m_undoStack.takeLast();
        // 移除需要撤销的图形项
        foreach (QGraphicsItem *item, itemsToRemove) {
            removeItem(item);
        }
        // 发出信号表明场景已更改
        emit drawingchanged();
    }
}

void DrawingScene::redo()
{
    if (canRedo()) {
        // 将当前图形项列表添加到撤销栈中
        m_undoStack.append(m_redoStack.last());
        // 从前进栈中弹出上一个图形项列表，并将其设置为当前图形项列表
        QList<QGraphicsItem*> itemsToAdd = m_redoStack.takeLast();
        // 将需要重做的图形项添加到场景中
        foreach (QGraphicsItem *item, itemsToAdd) {
            addItem(item);
        }
        // 发出信号表明场景已更改
        emit drawingchanged();
    }
}


bool DrawingScene::canUndo() const
{
    return !m_undoStack.isEmpty();
}

bool DrawingScene::canRedo() const
{
    return !m_redoStack.isEmpty();
}

void DrawingScene::setPenColor(const QColor &color)
{
    m_penColor = color;
}

void DrawingScene::setPenWidth(int width)
{
    m_penWidth = width;

}




void DrawingScene::updateView()
{
    if (m_view) {
        m_view->update();
    }
}
